package com.luyuan.crm.formula.web;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.formula.model.Formula;
import com.luyuan.crm.formula.model.Inventory;
import com.luyuan.crm.formula.service.FormulaService;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;

@Scope("view")
@Component("formulaAction")
public class FormulaAction extends BaseAction {

    private Formula criteria = new Formula();

    private Formula formula;

    private String products;

    @Autowired
    private FormulaService formulaService;

    public DataModel getTableModel() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Formula.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        c.add(r.like("name", criteria.getName()));
        if (criteria.getProductType() != null) {
            c.add(r.eq("productType", criteria.getProductType()));
        }
        c.addOrder(Order.desc("createTime"));
        Page page = formulaService.pagedQuery(c, limit.getPage(), limit.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public Formula getCriteria() {
        return criteria;
    }

    public Formula getFormula() {
        String formulaId = getRequestParameter("formulaId");
        products = "";

        if (formulaId != null && !"".equals(formulaId)) {
            formula = formulaService.get(formulaId);
            String temp = "";
            for (Inventory inventory : formula.getInventorys()) {
                double total = inventory.getProduct().getPrice() * inventory.getQuantity();
                if (!inventory.isGift()) {
                    products += inventory.getProduct().getName() + "x" + inventory.getQuantity() + "="
                            + inventory.getProduct().getPrice() + "x" + inventory.getQuantity() + "=" + total + " \r\n";
                }
                else {
                    temp += "赠品：" + inventory.getProduct().getName() + "x" + inventory.getQuantity() + "（价值" + total
                            + "）\r\n";
                }
            }
            this.products += temp;
        }

        if (formula == null) {
            formula = new Formula();
        }
        return formula;
    }

    public String showFormulaDetail(String formulaId) {
        if (formulaId != null) {
            formula = formulaService.get(formulaId);
            products = "";
            String temp = "";
            for (Inventory inventory : formula.getInventorys()) {
                double total = inventory.getProduct().getPrice() * inventory.getQuantity();
                if (!inventory.isGift()) {
                    products += inventory.getProduct().getName() + "x" + inventory.getQuantity() + "="
                            + inventory.getProduct().getPrice() + "x" + inventory.getQuantity() + "=" + total + " \r\n";
                }
                else {
                    temp += "赠品：" + inventory.getProduct().getName() + "x" + inventory.getQuantity() + "（价值" + total
                            + "）\r\n";
                }
            }
            this.products += temp;
        }
        return "/luyuan/formula/detailFormula.jsf";
    }

    public void addFormula() {
        formulaService.save(formula);
        addGlobalI18nMessage("添加配方成功");
    }

    public void updateFormula() {
        formulaService.update(formula);
        addGlobalI18nMessage("修改配方成功");
    }

    public void deleteByFormulaId(String formulaId) {
        if (formulaId == null) {
            addGlobalI18nMessage("配方ID不能为空！");
            return;
        }
        formulaService.deleteByFormulaId(formulaId);
        addGlobalI18nMessage("删除成功");
    }

    // public void batchDelete(){
    // String[] selectedIds = getRequestParameterValues("id");
    // if (selectedIds == null){
    // selectedIds = new String[0];
    // addGlobalI18nMessage("请至少选择一条数据");
    // return;
    // }
    // formulaService.batchDeleteFormula((Serializable[])selectedIds);
    // addGlobalI18nMessage("批量删除成功");
    // }

    public String getProducts() {
        return products;
    }

    public void setProducts(String products) {
        this.products = products;
    }

    public void enableByFormulaId(String formulaId) {
        formulaService.enableByFormulaId(formulaId);
        addGlobalI18nMessage("重新销售产品成功!");
    }

    public void disableByFormulaId(String formulaId) {
        formulaService.disableByFormulaId(formulaId);
        addGlobalI18nMessage("停止销售产品成功!");
    }
}
